package com.edu.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * UserDetail entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.edu.hibernate.UserDetail
 * @author MyEclipse Persistence Tools
 */

public class UserDetailDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UserDetailDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String ADDRESS = "address";
	public static final String EMAIL = "email";
	public static final String TEL = "tel";
	public static final String MOBILE = "mobile";
	public static final String GENDER = "gender";
	public static final String STATUS = "status";
	public static final String CREATE_BY = "createBy";

	public void save(UserDetail transientInstance) {
		log.debug("saving UserDetail instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(UserDetail persistentInstance) {
		log.debug("deleting UserDetail instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public UserDetail findById(java.lang.String id) {
		log.debug("getting UserDetail instance with id: " + id);
		try {
			UserDetail instance = (UserDetail) getSession().get(
					"com.edu.hibernate.UserDetail", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(UserDetail instance) {
		log.debug("finding UserDetail instance by example");
		try {
			List results = getSession().createCriteria(
					"com.edu.hibernate.UserDetail").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserDetail instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from UserDetail as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List findByMobile(Object mobile) {
		return findByProperty(MOBILE, mobile);
	}

	public List findByGender(Object gender) {
		return findByProperty(GENDER, gender);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findByCreateBy(Object createBy) {
		return findByProperty(CREATE_BY, createBy);
	}

	public List findAll() {
		log.debug("finding all UserDetail instances");
		try {
			String queryString = "from UserDetail";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public UserDetail merge(UserDetail detachedInstance) {
		log.debug("merging UserDetail instance");
		try {
			UserDetail result = (UserDetail) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(UserDetail instance) {
		log.debug("attaching dirty UserDetail instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(UserDetail instance) {
		log.debug("attaching clean UserDetail instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}